import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:get/get.dart';
import 'package:school_assistant_m/api/course/course_api.dart';
import 'package:school_assistant_m/utils/my_message_hint.dart';
import 'package:school_assistant_m/utils/my_toast.dart';

class AddCourseController extends GetxController with StateMixin {
  //TODO: Implement AddCourseController
  CourseApi courseApi = Get.find<CourseApi>();
  GlobalKey<FormState> addCourseKey = GlobalKey<FormState>();
  TextEditingController desc = TextEditingController();
  TextEditingController name = TextEditingController();
  TextEditingController courseType = TextEditingController();
  var schoolTime = {
    // 开始时间
    DateMode.YMDHMS: '',
    DateMode.YMDHM: '',
    DateMode.YMDH: '',
    DateMode.YMD: '',
    DateMode.YM: '',
    DateMode.Y: '',
    DateMode.MDHMS: '',
    DateMode.HMS: '',
    DateMode.MD: '',
    DateMode.S: '',
  }.obs;
  var finishTime = {
    // 结束时间
    DateMode.YMDHMS: '',
    DateMode.YMDHM: '',
    DateMode.YMDH: '',
    DateMode.YMD: '',
    DateMode.YM: '',
    DateMode.Y: '',
    DateMode.MDHMS: '',
    DateMode.HMS: '',
    DateMode.MD: '',
    DateMode.S: '',
  }.obs;
  var isVerify = false.obs; // 是否
  DateMode mode = DateMode.YMDHMS; // 时间模式，默认：“年月日时分”
  Map courseForm = {
    'name': '',
    'courseType': '',
    'desc': '',
    'schoolTime': '',
    'finishTime': '',
  };

  // 执行提交课程函数
  void handleSubmit() {
    var form = addCourseKey.currentState;
    if (form!.validate()) {
      form.save();
      courseForm['name'] = name.text;
      courseForm['courseType'] = courseType.text;
      courseForm['desc'] = desc.text;
      addCourse();
    }
  }

  Future<void> addCourse() async {
    var res = await courseApi.addCourse(courseForm);
    print('HHHHHHHHH---------------HHHHHHHHHHHHH');
    print(res);
  }


  /// 必填验证
  bool validate() {
    if (name == '' || schoolTime[mode] == '' || finishTime[mode] == ''||desc==''||courseType=='') {
      isVerify.value = true;
      return false;
    }
    return true;
  }
  @override
  void onInit() {
    super.onInit();
  }
}
